package so.ifl.manager.main;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.Constant;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.MD5;
import so.ifl.commons.tools.StringHelper;
import so.ifl.servcie.AccountService;
import so.ifl.servcie.MenuService;
import so.ifl.servcie.RoleService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.MenuVo;

/**
 *create by aaron.lu
 *create at 2011-12-20 下午11:04:08
 *<mailto: atyuan.lu@gmail.com>
 **/
@Component("mainManager")
public class MainManager {
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private AccountService accountService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	/**
	 * 页面菜单递归查询
	 * create by aaron.lu
	 * create at 2012-5-2 下午11:36:41
	 * <mailto: atyuan.lu@gmail.com>
	 * @param parentMenuId
	 * @return
	 *
	 */
	@DataProvider
	public List<MenuVo> getMenuByAccount(Integer parentMenuId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("men_menu_id_", parentMenuId);
		params.put("is_do_", 1);
		List<MenuVo> list  = null;
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo) {
			boolean need_query = false;
			if (accountVo.getAccountLoginName().equals(Constant.SYSTEM_ACCOUNT_LOGIN_NAME)) {
				need_query = true;
			}
			if (StringHelper.isNotEmpty(accountVo.getHasRoles())) {
				params.put("has_role", accountVo.getHasRoles());
				need_query = true;
			}
			
			if (need_query) {
				list = menuService.queryMenuWithRole(params, 0, 100);				
				logger_.debug("getMenuByAccount " + list.size() + " menu gets...");
			}
		}	
		return list;
		
	}
	
	/**
	 * 非递归查询菜单
	 * create by aaron.lu
	 * create at 2012-1-15 下午10:21:34
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getMenuWithParam(Page<MenuVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limit = page.getPageSize();
		
		int total = menuService.countMenuWithParamNoNull(params);
		List<MenuVo> list = menuService.queryMenuWithParamAndPaginationNoNull(params, offset, limit);
		
		logger_.debug(list.size() + " menu gets...");
		
		page.setEntityCount(total);
		page.setEntities(list);	
		
	}
	
	/**
	 * 通过权限查询菜单<权限的菜单查询>
	 * create by aaron.lu
	 * create at 2012-1-17 上午11:11:50
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param params
	 *
	 */
	@DataProvider
	public void getMenuWithRole(Page<MenuVo> page, Map<String, Object> params){
		int offset = page.getFirstEntityIndex();
		int limit = page.getPageSize();
		
		int total = menuService.countMenuWithRoleNoRecurrence(params);
		List<MenuVo> list = menuService.queryMenuWithRoleNoRecurrence(params, offset, limit);
		
		logger_.debug("getMenuWithRole " + list.size() + " menu gets...");
		
		page.setEntityCount(total);
		page.setEntities(list);		
	}
	
	/**
	 * 保存菜单信息
	 * create by aaron.lu
	 * create at 2012-1-15 下午11:57:31
	 * <mailto: atyuan.lu@gmail.com>
	 * @param menuVos
	 *
	 */
	@DataResolver
	public void saveMenu(Collection<MenuVo> menuVos){
		for (MenuVo menuVo : menuVos) {
			EntityState entityState = EntityUtils.getState(menuVo);
			
			if (EntityState.NEW.equals(entityState)) {
				//新增
				 menuService.insertMenu(menuVo);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				//修改-菜单无需进行修改
			} else if (EntityState.DELETED.equals(entityState)) {
				//删除
				menuService.deleteMenu(menuVo.getMenuId());
			}
		}
	}
	
	/**
	 * 获取当前用户信息
	 * create by aaron.lu
	 * create at 2012-2-19 下午4:27:56
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> getUserInfo(){
		Map<String, Object> result = new HashMap<String, Object>();
		String successFlag = Constant.SUCCESS_FALSE_FLAG;
		String msg = "anonym";
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			successFlag = Constant.SUCCESS_TRUE_FLAG;
			msg = accountVo.getAccountOwner();
			
		}else{
			
			//do notthing
		}
		
		
		
		result.put("flag", successFlag);
		result.put("msg", msg);
		
		return result;
	}
	
	
	/**
	 * 退出系统
	 * create by aaron.lu
	 * create at 2012-2-19 下午4:32:04
	 * <mailto: atyuan.lu@gmail.com>
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> logout(){
		Map<String, Object> result = new HashMap<String, Object>();
		String successFlag = Constant.SUCCESS_FALSE_FLAG;
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			DoradoSystemUtils.setNullCurrentUser(DoradoContext.getCurrent().getRequest());
			successFlag = Constant.SUCCESS_TRUE_FLAG;
			
		}else{
			
			successFlag = Constant.SUCCESS_TRUE_FLAG;
		}
		
		
		
		result.put("flag", successFlag);
		
		return result;
	}
	
	/**
	 * 修改密码
	 * create by aaron.lu
	 * create at 2012-5-24 下午11:05:02
	 * <mailto: atyuan.lu@gmail.com>
	 * @param oldPwd
	 * @param newPwd
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> changPwd(String oldPwd, String newPwd){
		Map<String, Object> result = new HashMap<String, Object>();
		String successFlag = Constant.SUCCESS_FALSE_FLAG;
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			if(null != accountVo.getAccountPwd() && accountVo.getAccountPwd().equals(MD5.getMD5(oldPwd))){
				accountVo.setAccountPwd(MD5.getMD5(newPwd));	
				
				Map<String, Object> updateParams = new HashMap<String, Object>();
				updateParams.put("account_pwd_", accountVo.getAccountPwd());
				updateParams.put("accout_id_", accountVo.getAccoutId());
				
				accountService.updateAccount(updateParams);
				successFlag = Constant.SUCCESS_TRUE_FLAG;
			}			
						
		}else{
			// let the flag is false
		}
		result.put("flag", successFlag);
		
		return result;
	}
	
	/**
	 * 保存权限修改
	 * create by aaron.lu
	 * create at 2012-1-17 下午8:57:15
	 * <mailto: atyuan.lu@gmail.com>
	 * @param menuId
	 * @param roleId
	 * @param isAdd
	 * @return
	 *
	 */
	@Expose
	public Map<String, Object> saveEditRelation(String menuId, String roleId, String isAdd) {
		Map<String, Object> result = new HashMap<String, Object>();
		String successFlag = Constant.SUCCESS_FALSE_FLAG;
		String msg = "";
		
		
		if (StringHelper.isNotEmpty(menuId) && StringHelper.isNotEmpty(roleId)
				&& StringHelper.isNotEmpty(isAdd)) {
			if (isAdd.equals("1")) {
				//新增关系
				roleService.insertRelationRole2Menu(menuId, roleId);
			}else if(isAdd.equals("0")){
				roleService.deleteByMRID(menuId, roleId);
			}
		}
		
		result.put("flag", successFlag);
		result.put("msg", msg);
		
		return result;
	}
	

}
